/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sendtion.xrichtextdemo.utils;

import com.sendtion.xrichtextdemo.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * 自定义Toast
 *
 * @since 2021-06-24
 */
public class MyToast {
    /**
     * LENGTH_SHORT
     *
     */
    public static final int LENGTH_SHORT = 2000;
    private static final int INT_16 = 16;
    private static final int INT_12 = 16;
    private static final int INT_10 = 10;
    private static final int INT_20 = 20;
    private static ToastDialog toastDialog;

    /**
     * ToastLayout
     *
     * @since 2021-06-24
     */
    public enum ToastLayout {
        /**
         * CENTER
         *
         */
        CENTER,
        /**
         * TOP
         *
         */
        TOP,
        /**
         * BOTTOM
         *
         */
        BOTTOM,
    }

    /**
     * show
     *
     * @param context
     * @param content
     * @param layout
     */
    public static void show(Context context, String content, ToastLayout layout) {
        createTost(context, content, LENGTH_SHORT, layout);
    }

    private static void createTost(Context context, String content, int duration, ToastLayout layout) {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter
                .getInstance(context).parse(ResourceTable.Layout_toast_layout, null, false);
        Text componentById = (Text) customToastLayout.findComponentById(ResourceTable.Id_tx);
        componentById.setText(content);
        componentById.setBackground(buildDrawableByColorRadius(Color.getIntColor("#FFE5E3E3"),
                vp2px(context, INT_20)));
        int mLayout = LayoutAlignment.CENTER;
        switch (layout) {
            case TOP:
                mLayout = LayoutAlignment.TOP;
                break;
            case BOTTOM:
                mLayout = LayoutAlignment.BOTTOM;
                break;
            case CENTER:
                mLayout = LayoutAlignment.CENTER;
                break;
            default:
                break;
        }
        if (toastDialog != null) {
            toastDialog.cancel();
            toastDialog = null;
        }
        toastDialog = new ToastDialog(context);
        toastDialog.setComponent(customToastLayout);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(mLayout);
        toastDialog.setTransparent(true);
        toastDialog.setDuration(duration);
        toastDialog.show();
    }

    private static ohos.agp.components.element.Element buildDrawableByColorRadius(int color, float radius) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(0);
        drawable.setRgbColor(RgbColor.fromArgbInt(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }

    private static int vp2px(Context context, float vp) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return (int) (attributes.densityPixels * vp);
    }
}
